package com.bw.home

import android.os.Build
import android.view.View
import androidx.annotation.RequiresApi
import androidx.core.widget.NestedScrollView
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.bw.base.constants.RoutePath
import com.bw.common.entity.TabEntity
import com.bw.common.view.BaseMvvmFragment
import com.bw.common.adapter.BannerResAdapter
import com.bw.home.adapter.HomeCementAdapter
import com.bw.home.adapter.HotServiceAdapter
import com.bw.home.databinding.FragmentHomeBinding
import com.bw.home.model.HomeCateData
import com.youth.banner.indicator.CircleIndicator
import com.youth.banner.transformer.AlphaPageTransformer


@Route(path = RoutePath.PATH_HOME_FRAGMENT)
class HomeFragment : BaseMvvmFragment<FragmentHomeBinding, HomeViewModel>() {
    private lateinit var adapter: HomeCementAdapter
    private val searchHotWords = arrayListOf("搜索园区服务", "节日彩蛋", "园区新闻", "搜索月卡会员")
    private val bannerList = mutableListOf<TabEntity>(

        TabEntity(selImgDrawable = R.drawable.home_city),
        TabEntity(selImgDrawable = R.drawable.home_city2),
        TabEntity(selImgDrawable = R.drawable.home_city3),
    )
    private lateinit var bannerAdapter: BannerResAdapter
    private lateinit var hotServiceAdapter: HotServiceAdapter
    private var pageIndex: Int = 1
    private var pageSize: Int = 10
    private var isLoadEnd:Boolean = false


//    private val  mViewModel:HomeViewModel by activityViewModels()


    @RequiresApi(Build.VERSION_CODES.M)
    override fun initView() {

        binding.include.cardTx.visibility = View.VISIBLE
        binding.include.imgBell.visibility = View.VISIBLE



        hotServiceAdapter = HotServiceAdapter(HomeCateData.serviceCategoryList)
        binding.rvCate.adapter = hotServiceAdapter
        binding.rvCate.layoutManager = GridLayoutManager(context, 4)
        hotServiceAdapter.setOnItemClickListener(object : HotServiceAdapter.OnItemClickListener {
            override fun onItemClick(entity: TabEntity, position: Int) {
                when (entity.title) {
                    "车位管理" -> {
                        ARouter.getInstance().build(RoutePath.PATH_PACK_CAR).navigation()
                    }
                    "访客登记" -> {
                        ARouter.getInstance().build(RoutePath.PATH_VISITOR).navigation()
                    }
                    "物业报修" -> {
                        ARouter.getInstance().build(RoutePath.PATH_REPAIR).navigation()
                    }
                    "考勤打卡" -> {
                        ARouter.getInstance().build(RoutePath.PATH_CLOCK).navigation()
                    }
                    "房源信息" -> {
                        ARouter.getInstance().build(RoutePath.PATH_HOUSE).navigation()
                    }
                    "商家推荐" -> {
                        ARouter.getInstance().build(RoutePath.PATH_MERCHANT).navigation()
                    }
                    "月卡会员" -> {
                        ARouter.getInstance().build(RoutePath.PATH_MEMBER).navigation()
                    }
                    "设备管理" -> {
                        ARouter.getInstance().build(RoutePath.PATH_DEVICE).navigation()
                    }
                }
            }
        })



        adapter = HomeCementAdapter()
        binding.rv.adapter = adapter
        binding.rv.layoutManager = LinearLayoutManager(activity)
        binding.rv.setHasFixedSize(true)
        binding.rv.setItemViewCacheSize(10)

        binding.tbv.setDatas(searchHotWords)
        binding.tbv.setItemOnClickListener { data, position ->
            ToastUtils.showShort(searchHotWords[position])
        }
        pageIndex = 1
        vm.getCements(pageIndex, pageSize)

        bannerAdapter = BannerResAdapter(bannerList)
        binding.banner.run {
            //关联生命周期
            addBannerLifecycleObserver(this@HomeFragment.viewLifecycleOwner)
            setAdapter(bannerAdapter)
            indicator = CircleIndicator(this@HomeFragment.requireContext())
            setPageTransformer(AlphaPageTransformer())
        }


    }


    override fun initData() {

        vm.cementSuccess.observe(this) {
            if (it.isNotEmpty()) {
                adapter.data.addAll(it.filter { cementEntity -> cementEntity.title.isNotEmpty() })
                adapter.notifyDataSetChanged()
            } else {
                isLoadEnd = true
                ToastUtils.showShort("已经加载完了")

            }
        }

        binding.nsv.run {
            setOnScrollChangeListener(NestedScrollView.OnScrollChangeListener { vv, scrollX, scrollY, oldScrollX, oldScrollY ->
                if (scrollY == (vv.getChildAt(0).measuredHeight) - vv.measuredHeight) {
                    if(!isLoadEnd){
                        pageIndex++
                        vm.getCements(pageIndex, pageSize)
                    }
                }
            })
        }

    }

    override fun onDestroy() {
        super.onDestroy()

    }


}